package gl.android.biyiqing.competent;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.huewu.pla.lib.internal.PLA_AdapterView;

import gl.android.biyiqing.BYQBaseActivity;
import gl.android.biyiqing.UserInfoActivity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

public class OnToUserInfoItemClickListener implements OnItemClickListener,
		com.huewu.pla.lib.internal.PLA_AdapterView.OnItemClickListener {

	private Context mContext;
	private String jsonArrayData;
	private String id;

	public OnToUserInfoItemClickListener(Context mContext,
			String jsonArrayData, String[] from) {
		this.mContext = mContext;
		this.jsonArrayData = jsonArrayData;
	}

	public OnToUserInfoItemClickListener(Context mContext, String id) {
		this.mContext = mContext;
		this.id = id;
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		goUserInfoActivity(arg2);
	}

	private void goUserInfoActivity(int arg2) {
		Intent intent = new Intent(mContext, UserInfoActivity.class);
		if (jsonArrayData == null && id != null) {
			intent.putExtra(BYQBaseActivity.KEY_USER_ID, id);
			mContext.startActivity(intent);
		} else {
			Type stringStringMap = new TypeToken<ArrayList<Map<String, String>>>() {
			}.getType();
			List<Map<String, String>> map = new Gson().fromJson(jsonArrayData,
					stringStringMap);
			intent.putExtra(BYQBaseActivity.KEY_USER_ID,
					map.get(arg2).get(BYQBaseActivity.KEY_USER_ID));
			mContext.startActivity(intent);
		}
	}

	@Override
	public void onItemClick(PLA_AdapterView<?> parent, View view, int position,
			long id) {
		goUserInfoActivity(position);
	}

}
